package com.jucai.pppeiyouclient.view.activity;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;

import com.jucai.pppeiyouclient.R;
import com.jucai.pppeiyouclient.base.BaseActivity;
import com.jucai.pppeiyouclient.comm.CommonMethod;
import com.jucai.pppeiyouclient.utils.ActivityCollector;
import com.jucai.pppeiyouclient.utils.LogUtils;
import com.jucai.pppeiyouclient.utils.TextCheckUtils;
import com.jucai.pppeiyouclient.view.popup.EduChoosePopup;
import com.jucai.pppeiyouclient.view.popup.SexChoosePopup;

import java.util.Calendar;

public class OrderFilterActivity extends BaseActivity {
    private TextView filterSex, filterStartTime, filterEdu, filterWithCar, filterWithCamera, filterSubmit;
    private EditText filterAge, filterCareer;
    private SexChoosePopup sexChoosePopup;
    private EduChoosePopup eduChoosePopup;
    private boolean withCar = false;
    private boolean withCamera = false;
    private Calendar calendar;
    private int year, month, day;

    private String filterCode = "";
    private String sexBack, ageBack, timeBack, eduBack;


    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_order_filter);
        ((TextView) findViewById(R.id.common_title_text)).setText("接单");
        findViewById(R.id.common_home_back).setVisibility(View.INVISIBLE);
        filterSex = (TextView) findViewById(R.id.filter_sex);
        filterStartTime = (TextView) findViewById(R.id.filter_start_time);
        filterEdu = (TextView) findViewById(R.id.filter_edu);
        filterWithCar = (TextView) findViewById(R.id.filter_with_car);
        filterWithCamera = (TextView) findViewById(R.id.filter_with_camera);
        filterSubmit = (TextView) findViewById(R.id.filter_submit);
        filterAge = (EditText) findViewById(R.id.filter_age);
        filterCareer = (EditText) findViewById(R.id.filter_career);
    }

    @Override
    protected void initData() {

        sexBack = getIntent().getStringExtra("sex");
        ageBack = getIntent().getStringExtra("minage");
        timeBack = getIntent().getStringExtra("start_time");
        eduBack = getIntent().getStringExtra("education");

        calendar = Calendar.getInstance();
        year = calendar.get(Calendar.YEAR);
        month = calendar.get(Calendar.MONTH);
        day = calendar.get(Calendar.DAY_OF_MONTH);

        if (withCar) {
            filterWithCar.setBackgroundResource(R.drawable.shape_left_half_circle_orange);
        } else {
            filterWithCar.setBackgroundResource(R.drawable.shape_left_half_circle_gray);
        }

        if (withCamera) {
            filterWithCamera.setBackgroundResource(R.drawable.shape_right_half_circle_orange);
        } else {
            filterWithCamera.setBackgroundResource(R.drawable.shape_right_half_circle_gray);
        }

        switch (sexBack.trim()) {
            case "0":
                filterSex.setText("女");
                break;
            case "1":
                filterSex.setText("男");
                break;
            default:
                break;
        }
        if (!ageBack.trim().equals(""))
            filterAge.setText(ageBack);
        if (!timeBack.trim().equals(""))
            filterStartTime.setText(timeBack);

        sexChoosePopup = new SexChoosePopup(OrderFilterActivity.this, getWindow(), R.style.animBottomInBottomOut, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sexChoosePopup.dismiss();
                switch (view.getId()) {
                    case R.id.popup_male:
                        filterSex.setText("男");
                        break;
                    case R.id.popup_female:
                        filterSex.setText("女");
                        break;
                    default:
                        break;
                }
            }
        });

        switch (eduBack.trim()) {
            case "0":
                filterEdu.setText("不限");
                break;
            case "1":
                filterEdu.setText("初中");
                break;
            case "2":
                filterEdu.setText("高中");
                break;
            case "3":
                filterEdu.setText("专科");
                break;
            case "4":
                filterEdu.setText("本科");
                break;
            case "5":
                filterEdu.setText("硕士");
                break;
            case "6":
                filterEdu.setText("博士");
                break;
            default:
                break;
        }

        if (eduBack.trim().equals(""))
            filterCode = "0";
        else
            filterCode = eduBack.trim();

        eduChoosePopup = new EduChoosePopup(this, getWindow(), R.style.animBottomInBottomOut, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                eduChoosePopup.dismiss();
                switch (view.getId()) {
                    case R.id.edu_doctor:
                        filterEdu.setText("博士");
                        filterCode = "6";
                        break;
                    case R.id.edu_master:
                        filterEdu.setText("硕士");
                        filterCode = "5";
                        break;
                    case R.id.edu_undergraduate:
                        filterEdu.setText("本科");
                        filterCode = "4";
                        break;
                    case R.id.edu_junior:
                        filterEdu.setText("专科");
                        filterCode = "3";
                        break;
                    case R.id.edu_high_school:
                        filterEdu.setText("高中");
                        filterCode = "2";
                        break;
                    case R.id.edu_middle_school:
                        filterEdu.setText("初中");
                        filterCode = "1";
                        break;
                    case R.id.edu_other:
                        filterEdu.setText("不限");
                        filterCode = "0";
                        break;
                    default:
                        break;
                }
            }
        });
    }

    @Override
    protected void loadNetData() {

    }

    @Override
    protected void setListener() {
        findViewById(R.id.common_back).setOnClickListener(this);
        findViewById(R.id.common_home_back).setOnClickListener(this);
        filterSex.setOnClickListener(this);
        filterStartTime.setOnClickListener(this);
        filterEdu.setOnClickListener(this);
        filterWithCar.setOnClickListener(this);
        filterWithCamera.setOnClickListener(this);
        filterSubmit.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.common_back:
                ActivityCollector.removeActivity(this);
                break;
            case R.id.common_home_back:
                ActivityCollector.removeActivity(this);
                break;
            case R.id.filter_sex:
                sexChoosePopup.showAtLocation(view, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
                CommonMethod.setWindowAlpha(0.5f, getWindow());
                break;

            case R.id.filter_start_time:
                new DatePickerDialog(OrderFilterActivity.this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {
                        String strMonth = "";
                        String strDay = "";

                        if (String.valueOf(monthOfYear).length() < 2) {
                            strMonth = "0" + (monthOfYear + 1);
                        } else {
                            strMonth = "" + (monthOfYear + 1);
                        }

                        if (String.valueOf(dayOfMonth).length() < 2) {
                            strDay = "0" + dayOfMonth;
                        } else {
                            strDay = "" + dayOfMonth;
                        }

                        filterStartTime.setText(year + "-" + strMonth + "-" + strDay);

                    }
                }, year, month, day).show();
                break;

            case R.id.filter_edu:
                eduChoosePopup.showAtLocation(view, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
                CommonMethod.setWindowAlpha(0.5f, getWindow());
                break;

            case R.id.filter_with_car:
                withCar = !withCar;
                if (withCar) {
                    filterWithCar.setBackgroundResource(R.drawable.shape_left_half_circle_orange);
                } else {
                    filterWithCar.setBackgroundResource(R.drawable.shape_left_half_circle_gray);
                }
                break;

            case R.id.filter_with_camera:
                withCamera = !withCamera;
                if (withCamera) {
                    filterWithCamera.setBackgroundResource(R.drawable.shape_right_half_circle_orange);
                } else {
                    filterWithCamera.setBackgroundResource(R.drawable.shape_right_half_circle_gray);
                }
                break;

            case R.id.filter_submit:

                if (filterSex.getText().toString().equals("请选择性别")) {
                    sexBack = "";
                } else {
                    if (filterSex.getText().toString().equals("男")) {
                        sexBack = "1";
                    } else {
                        sexBack = "0";
                    }
                }

                if (TextCheckUtils.isEmpty(filterAge.getText().toString())) {
                    ageBack = "";
                } else {
                    ageBack = filterAge.getText().toString();
                }

                if (filterStartTime.getText().toString().equals("请选择出发时间")) {
                    timeBack = "";
                } else {
                    timeBack = filterStartTime.getText().toString();
                }


                if (filterEdu.getText().toString().equals("请选择学历")) {
                    eduBack = "";
                } else {
                    eduBack = filterCode;
                }

                /*
                if (!TextUtils.isEmpty(filterCareer.getText().toString())) {
                    careerBack = filterCareer.getText().toString();
                }
                */

                /*
                carStateBack = withCar ? "1" : "0";
                cameraStateBack = withCamera ? "1" : "0";
                */

                Intent filterIntent = new Intent();
                filterIntent.putExtra("filter_sex", sexBack);
                filterIntent.putExtra("filter_age", ageBack);
                filterIntent.putExtra("filter_time", timeBack);
                filterIntent.putExtra("filter_edu", eduBack);

                LogUtils.d("Filter 筛选条件：" + sexBack + "|" + ageBack + "|" + timeBack + "|" + eduBack);

                /*
                filterIntent.putExtra("filter_career", careerBack);
                filterIntent.putExtra("filter_car", carStateBack);
                filterIntent.putExtra("filter_camera", cameraStateBack);
                */
                setResult(RESULT_OK, filterIntent);
                ActivityCollector.removeActivity(OrderFilterActivity.this);
                break;
        }
    }
}
